Load the necessary packages and data.
library(tidyverse)
library(plotly)
library(httr)
library(jsonlite)
get_all_inspections = function(url) {
all_inspections = vector("list", length = 0)
loop_index = 1
chunk_size = 50000
DO_NEXT = TRUE
while (DO_NEXT) {
message("Getting data, page ", loop_index)
all_inspections[[loop_index]] =
GET(url,
query = list(`$order` = "zipcode",
`$limit` = chunk_size,
`$offset` = as.integer((loop_index - 1) * chunk_size)
)
) %>%
content("text") %>%
fromJSON() %>%
as_tibble()
DO_NEXT = dim(all_inspections[[loop_index]])[1] == chunk_size
loop_index = loop_index + 1
}
all_inspections
}
url = "https://data.cityofnewyork.us/resource/43nn-pn8j.json"
nyc_inspections =
get_all_inspections(url) %>%
bind_rows()
nyc_inspections |>
drop_na() |>
filter(boro == "Manhattan") |>
mutate(
text_label = str_c("Grade: ", grade, "\nCritical Flag: ", critical_flag, "\nViolation: ", violation_description),
long = as.numeric(latitude),
lat = as.numeric(longitude)) |>
plot_ly(
x = ~lat, y = ~long, type = "scatter", mode = "markers", color = ~grade, text = ~text_label, alpha = 0.5, colors = "viridis")
nyc_inspections |>
drop_na() |>
mutate(
score = as.numeric(score),
boro = fct_reorder(boro, score)) |>
plot_ly(x = ~boro, y = ~score, color = ~boro, type = "box", colors = "viridis")
nyc_inspections |>
drop_na() |>
filter(boro == "Manhattan") |>
count(cuisine_description) |>
mutate(cuisine_description = fct_reorder(cuisine_description, n)) |>
filter(n > 30) |>
plot_ly(x = ~cuisine_description, y= ~n, color = ~cuisine_description, type = "bar", colors = "viridis")